// src/mock/index.js
import MockAdapter from 'axios-mock-adapter'
import userMocks from './modules/user'
import caseMocks from './modules/case'

export const setupMock = (axiosInstance) => {
    return new Promise((resolve) => {
        // 模拟延迟加载
        setTimeout(() => {
            const mock = new MockAdapter(axiosInstance, {
                delayResponse: Number(import.meta.env.VITE_MOCK_DELAY || 500),
                onNoMatch: 'throwException'
            })

            // 初始化用户模块
            userMocks(mock)
            // 初始化案件模块
            caseMocks(mock)

            // 添加全局API路径匹配
            mock.onAny(/^\/api\//).reply(config => {
                console.error(`[Mock Error] 未定义的API请求: ${config.method} ${config.url}`)
                return [404, {
                    code: 404,
                    message: 'Mock未定义此接口',
                    path: config.url,
                    method: config.method
                }]
            })

            console.log('[Mock] Mock服务初始化完成')
            resolve(mock)
        }, 300) // 添加300ms延迟模拟异步加载
    })
}

// 保留默认导出以保持向后兼容
export default function(axiosInstance) {
    return setupMock(axiosInstance)
}